package com.bvbot.core.bot;

import static com.bvbot.core.Logger.log;

import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class VolatilityBotConfig extends AbstractBotConfig {

	private float maxChangeLimit;

	private final Map<Integer, Float> maxChangeByMinutes = new TreeMap<Integer, Float>();

	public float getMaxChangeLimit() {
		return maxChangeLimit;
	}

	public void setMaxChangeLimit(float maxDecrease) {
		this.maxChangeLimit = maxDecrease;
	}

	@Override
	public String toString() {
		return (isGold() ? "Gold bot with " : "Silver bot with ")
				+ "maxDecreaseLimit=" + getMaxChangeLimit()
				+ toString(maxChangeByMinutes);
	}

	private String toString(Map<Integer, Float> tutu) {
		StringBuilder result = new StringBuilder();
		if (maxChangeByMinutes != null && !maxChangeByMinutes.isEmpty()) {
			for (Entry<Integer, Float> entry : maxChangeByMinutes.entrySet()) {
				result.append(";" + entry.getKey() + " minutes/"
						+ entry.getValue() + " change");
			}
		}
		return result.toString();
	}

	public Map<Integer, Float> getMaxChangeByMinutes() {
		return maxChangeByMinutes;
	}

	public void initMaxChangesByMinutes(String maxChangesByMinutesAsString) {
		if (maxChangesByMinutesAsString != null) {
			String[] splits = maxChangesByMinutesAsString.split("\\|");
			if (splits != null) {
				for (String split : splits) {
					if (split.length() > 0) {
						String[] keyValue = split.split(";");
						String nbMinutesAsString = keyValue[0];
						String limitAsString = keyValue[1];
						Integer nbMinutes = Integer.parseInt(nbMinutesAsString);
						Float limit = Float.parseFloat(limitAsString);
						log("Limit set at " + limit + " for " + nbMinutes
								+ " minutes.");
						maxChangeByMinutes.put(nbMinutes, limit);
					}
				}
			}
		}
	}

}
